<?php
class packing_list_packages extends ActiveRecord {
/**
 *
 * 	 * The followings are the available columns in table 'packing_list_packages':
 * @var integer $id
 * * @var integer $packing_details_id
 * * @var string $marking
 * * @var integer $pkg
 * * @var double $wt
 * * @var double $width
 * * @var double $length	 *
 *  @var double $height	 */	/**
 * 	 * Returns the static model of the specified AR class.
 * @return CActiveRecord the static model class	 */
    public $do_list = array() ,$cargo=array();
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }	/**	 * @return string the associated database table name	 */
    public function tableName() {
        return 'packing_list_packages';
    }	/**	 * @return array validation rules for model attributes.	 */
    public function rules() {
    //// NOTE: you should only define rules for those attributes that
    //// will receive user inputs.
        return array(
        array('marking, pkg, wt, width, length, height', 'required'),
        array('packing_list_id, pkg', 'numerical', 'integerOnly'=>true),
        array('wt, width, length, height', 'numerical'),
        array('marking', 'length', 'max'=>15),
        );
    }	/**	 * @return array relational rules.	 */
    public function relations() {
    //// NOTE: you may need to adjust the relation name and the related
    //// class name for the relations automatically generated below.
        return array(
        'cargo'=>array(self::HAS_MANY,'packing_list_packages_cargos','package_id'),
        'packing_list'=>array(self::BELONGS_TO,'packing_list','packing_list_id'),
        );
    }
    private function validatePackage(){
        $valid = true;
        if(!$this->validate())
            $valid = false;
        foreach($this->cargo as $_c) {
            if($_c->ref_no != '') {
               if(!$_c->validate())
               	$valid = false;
            }
        }
        return $valid;
    }
    public function savePackages($validate = true) {
        if(!$this->validatePackage() && $validate)
            return false;
        if($this->save()) {
            foreach($this->cargo as $_c) {
            	$_c->package_id = $this->id;
            	$_c->save(false);
            }
        }
        else
            return false;
        return true;
    }
    public function current_do_list() {
        $dos = array();
        if(!$this->getIsNewRecord()) {
            foreach($this->cargo() as $cargo) {
            	$label = $cargo->ref_no.' ('.$cargo->pkg.'/'.$cargo->getTotalQty().')';
                $dos[] = CHtml::link($label,array('cargo/update','id'=>$cargo->id),array('id'=>'cargo'.$cargo->id));
            }
            return implode($dos,' ');
        }
    }
/* @return array customized attribute labels (name=>label)	 */
    public function attributeLabels() {
        return array(
            'id' => 'Id',
        'packing_details_id' => 'Packing Details',
        'marking' => 'Marking',
        'pkg' => 'Packages',
        'wt' => 'Weight',
        'width' => 'Width',
        'length' => 'Length',
        'height' => 'Height',
        );
    }

}
